#include "GameProcessManager.h"

GameProcessManager::GameProcessManager(void)
{

}

GameProcessManager::~GameProcessManager(void)
{
	mProcessList.clear();
}

void GameProcessManager::attach(GameProcessPtr process)
{
	process->setActive(true);
	mProcessList.push_back(process);
}

void GameProcessManager::detach(GameProcessPtr process)
{
	mProcessList.remove(process);
}

void GameProcessManager::update(const unsigned long elapsedTime)
{
	ProcessList::iterator it = mProcessList.begin();
	ProcessList::iterator end = mProcessList.end();
	GameProcessPtr pNext;

	// Go through all processes
	while (it != end)
	{
		GameProcessPtr p(*it);
		++it;

		if (p->dead())
		{
			pNext = p->getNext();

			// Add next process to the process list
			if (pNext)
			{
				p->setNext(GameProcessPtr((GameProcess*)NULL));
				attach(pNext);
			}
				// Delete process
				detach(p);
			}
		else if (p->active())
		{
			// Update if active
			p->update(elapsedTime);
		}
	}
}